package org.fireflow2.designer.eclipse.modelwrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.fireflow.model.ModelElement;
import org.fireflow.pdl.fpdl20.diagram.DiagramElement;
import org.fireflow.pdl.fpdl20.process.Node;
import org.fireflow.pdl.fpdl20.process.Transition;
import org.fireflow2.designer.eclipse.modelwrapper.basic.TransitionWrapper;

public class NodeWrapper extends ModelWrapper {
	protected SubProcessWrapper subProcessWrapper = null;
	protected List<TransitionWrapper> leavingTransitions = new ArrayList<TransitionWrapper>();
	protected List<TransitionWrapper> enteringTransitions = new ArrayList<TransitionWrapper>();

	
	public NodeWrapper(SubProcessWrapper subflowWrapper ,ModelElement arg0,DiagramElement arg1) {
		super(arg0,arg1);
		this.subProcessWrapper = subflowWrapper;
	}

	public List<TransitionWrapper> getLeavingTransitions() {
		return leavingTransitions;
	}

	public SubProcessWrapper getSubProcessWrapper(){
		return this.subProcessWrapper;
	}

	public List<TransitionWrapper> getEnteringTransitions() {
		return enteringTransitions;
	}
	

	public void setLeavingTransition(TransitionWrapper transWrapper){
		this.leavingTransitions.add(transWrapper);
	}

	public void setEnteringTransition(TransitionWrapper transWrapper){
		this.enteringTransitions.add(transWrapper);
	}	

	
	public void addLeavingTransition(TransitionWrapper transWrapper){
		this.leavingTransitions.add(transWrapper);
		if (this.wfElement instanceof Node){
			((Node)this.wfElement).getLeavingTransitions().add((Transition)transWrapper.getWorkflowModelElement());
		}	
        fireStructureChange(LEAVING_TRANSITION, transWrapper);
	}

	
	
	public void removeLeavingTransition(TransitionWrapper transWrapper){
		for (int i=0;i<leavingTransitions.size();i++){
			TransitionWrapper wrapper = (TransitionWrapper)this.leavingTransitions.get(i);
			if (wrapper.getSn().equals(transWrapper.getSn())){
				leavingTransitions.remove(i);
				break;
			}
		}
		if (this.wfElement instanceof Node){
			Node synch = (Node)this.wfElement;
			/* 因getSn()报错，05-11
			for (int i=0;i<synch.getLeavingTransitions().size();i++){
				Transition trans = (Transition)synch.getLeavingTransitions().get(i);
				if (trans.getSn().equals(transWrapper.getSn())){
					synch.getLeavingTransitions().remove(i);
					break;
				}
			}
			*/
		}
        fireStructureChange(LEAVING_TRANSITION, transWrapper);
	}	

	
	public void addEnteringTransition(TransitionWrapper transWrapper){
		this.enteringTransitions.add(transWrapper);
		if (this.wfElement instanceof Node){
			((Node)this.wfElement).getEnteringTransitions().add((Transition)transWrapper.getWorkflowModelElement());
		}		
        fireStructureChange(ENTERING_TRANSITION, transWrapper);
	}
	

	
	public void removeEnteringTransition(TransitionWrapper transWrapper){
		for (int i=0;i<enteringTransitions.size();i++){
			TransitionWrapper wrapper = (TransitionWrapper)this.enteringTransitions.get(i);
			if (wrapper.getSn().equals(transWrapper.getSn())){
				enteringTransitions.remove(i);
				break;
			}
		}
		/* 因getSn()报错，05-11
		if (this.wfElement instanceof Node){
			Node synch = (Node)this.wfElement;
			for (int i=0;i<synch.getEnteringTransitions().size();i++){
				Transition trans = (Transition)synch.getEnteringTransitions().get(i);
				if (trans.getSn().equals(transWrapper.getSn())){
					synch.getEnteringTransitions().remove(i);
					break;
				}
			}
		}		
		*/
        fireStructureChange(ENTERING_TRANSITION, transWrapper);
	}
	

	public void addEventListener(String className) {
		//TODO 2.0待补充
//		if (this.wfElement == null)
//			return;
//		EventListener listener = new EventListener();
//		listener.setClassName(className);
//		this.wfElement.getEventListeners().add(listener);
//		this.fireStructureChange(EVENT_LISTENER, listener);
	}




	public void deleteEventListener(List deletedListeners) {
		//TODO 2.0待补充
//		if (this.wfElement == null)
//			return;
//		// if (index<0 ||
//		// index>this.wfElement.getEventListeners().size())return;
//		this.wfElement.getEventListeners().removeAll(deletedListeners);
//		this.fireStructureChange(EVENT_LISTENER, deletedListeners);
	}
	public void addExtendAttribute(String key, String value) {
		//TODO 2.0待补充
//		if (this.wfElement == null)
//			return;
//		Map extendAttributes = this.wfElement.getExtendedAttributes();
//		extendAttributes.put(key, value);
//		this.fireStructureChange(EVENT_LISTENER, key + "=" + value);
	}
	public Map getExtendedAttributes(){
		//TODO 2.0待补充
		return null;
//		if (this.wfElement == null)
//			return null;
//		Map extendAttributes = this.wfElement.getExtendedAttributes();
//		return extendAttributes;
	}
	
	
	public void deleteExtendAttribute(String key){
		//TODO 2.0待补充
//		if (this.wfElement == null)
//			return;
//		Map extendAttributes = this.wfElement.getExtendedAttributes();
//		extendAttributes.remove(key);
//		this.fireStructureChange(EVENT_LISTENER, "key is " + key);		
	}
	      
}
